package com.aat.dxfy.buddy.test03;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;

public class Test05 {

	public static void main(String[] args) {
		new Test05().test();
	}

	public void test() {
		try {
			Foo foo = new ByteBuddy().subclass(Foo.class)
					
					
					.method(named("foo"))
					.intercept(valued(new Hello()))
					
					
					.make()
					.load(getClass().getClassLoader()).getLoaded().newInstance();
			
			System.out.println(foo.bar());
			System.out.println(foo.foo());
			System.out.println(foo.foo(1));
			
			
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	private Implementation valued(Object obj) {
		return FixedValue.value(obj);
	}

	private ElementMatcher<? super MethodDescription> named(String string) {
		return ElementMatchers.named(string);
	}

}

class Hello{
	@Override
	public String toString() {
		return "hello";
	}
}